package logic

import (
	"context"
	"fmt"
	"teacher1/model/mysql"
	"teacher1/model/redis"
	"teacher1/pkg"

	"teacher1/teacherrpc/internal/svc"
	"teacher1/teacherrpc/teacherrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TeacherRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTeacherRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TeacherRegisterLogic {
	return &TeacherRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TeacherRegisterLogic) TeacherRegister(in *teacherrpc.TeacherRegisterRequest) (*teacherrpc.TeacherRegisterResponse, error) {
	// todo: add your logic here and delete this line
	//获取缓存中的短信验证码
	get := redis.Rdb.Get("sendSmsregister" + in.Mobile)
	//接收从表单传输过来的验证码
	if get.Val() != in.SendSmsCode {
		return nil, fmt.Errorf("当前短信验证码错误")
	}
	//注册
	teacher := mysql.Teacher{
		Mobile:   in.Mobile,
		Password: pkg.MD5(in.Mobile),
	}
	err := mysql.DB.Debug().Create(&teacher).Error
	if err != nil {
		return nil, err
	}
	return &teacherrpc.TeacherRegisterResponse{Id: int64(teacher.ID)}, nil
}
